<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20" class="el-row-table">
        <el-col :span="12"
          ><div class="grid-content bg-purple">
            <el-input
              placeholder="请输入内容"
              v-model="queryInfo.name"
              clearable
              @clear="getCourseList"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getCourseList"
              ></el-button>
            </el-input></div
        ></el-col>
        <!-- <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="addCourseDialogVisible = true"
              >添加课程</el-button
            >
          </div>
        </el-col> -->

        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="success" @click="getCourseList">刷新</el-button>
          </div>
        </el-col>
      </el-row>

      <!-- 课程列表区域 -->
      <el-table border stripe width="100%" :data="courseList">
        <!-- <el-table-column fixed prop="title" label="班级名称"> </el-table-column> -->
        <el-table-column fixed prop="id" label="编号"> </el-table-column>
        <el-table-column fixed prop="name" label="课程名称"> </el-table-column>
        <el-table-column fixed prop="school" label="学校"> </el-table-column>
        <el-table-column fixed prop="college" label="学院" width="200">
        </el-table-column>
        <!-- <el-table-column fixed prop="grade" label="年级" width="100">
        </el-table-column>
        <el-table-column fixed prop="semester" label="学期" width="160">
        </el-table-column>
         -->

        <!-- <el-table-column fixed label="开放班课">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.opened"
              @change="openChanged(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed label="结束班课">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.ended"
              @change="endChanged(scope.row)"
              :disabled="scope.row.ended"
            >
            </el-switch>
          </template>
        </el-table-column> -->
        <!-- <el-table-column fixed label="操作" width="200">
          <template slot-scope="scope"> -->
        <!-- 第二个是修改按钮 -->
        <!-- <el-tooltip
              effect="dark"
              content="修改课程"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="info"
                icon="el-icon-edit"
                circle
                @click="editCourseDialog(scope.row.id)"
              ></el-button>
            </el-tooltip> -->
        <!-- 第三是结束课程按钮 -->
        <!-- <el-tooltip
              effect="dark"
              content="查看签到任务"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-info"
                circle
                @click="toTask(scope.row.id)"
              ></el-button>
            </el-tooltip> -->
        <!-- 第五个是删除按钮 -->
        <!-- <el-tooltip
              effect="dark"
              content="删除课程"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="removeDialog(scope.row.id)"
              ></el-button>
            </el-tooltip> -->
        <!-- </template>
        </el-table-column> -->
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.current"
        :page-sizes="[5, 10]"
        :page-size="queryInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加课程对话框 -->
    <el-dialog
      title="添加班课"
      :visible.sync="addCourseDialogVisible"
      width="50%"
      @close="addCourseDialogClosed"
      center
    >
      <!-- 内容主体区 -->
      <el-form
        :model="addCourseForm"
        :rules="addCourseFormRules"
        ref="addCourseFormRef"
        label-width="100px"
      >
        <el-form-item label="班级名称" prop="title">
          <el-input v-model="addCourseForm.title"></el-input>
        </el-form-item>
        <el-form-item label="课程名称" prop="name">
          <el-input v-model="addCourseForm.name"></el-input>
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-select v-model="addCourseForm.grade">
            <el-option label="2020" value="2020"></el-option>
            <el-option label="2021" value="2021"></el-option>
            <el-option label="2022" value="2022"></el-option>
            <el-option label="2023" value="2023"></el-option>
            <el-option label="2024" value="2024"></el-option>
            <el-option label="2025" value="2025"></el-option>
            <el-option label="2026" value="2026"></el-option>
            <el-option label="2027" value="2027"></el-option>
            <el-option label="2028" value="2028"></el-option>
            <el-option label="2029" value="2029"></el-option>
            <el-option label="2030" value="2030"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学期" prop="semester">
          <el-select v-model="addCourseForm.semester">
            <el-option label="2020-2021-1" value="2020-2021-1"></el-option>
            <el-option label="2020-2021-2" value="2020-2021-2"></el-option>
            <el-option label="2021-2022-1" value="2021-2022-1"></el-option>
            <el-option label="2021-2022-2" value="2021-2022-2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-select v-model="addCourseForm.school" @focus="getSchoolList">
            <template v-for="school in schoolList">
              <el-option
                :label="school.name"
                :value="school.id"
                :key="school.id"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="学院" prop="collegeId">
          <el-select v-model="addCourseForm.collegeId" @focus="getCollegeList">
            <template v-for="college in collegeList">
              <el-option
                :label="college.name"
                :value="college.id"
                :key="college.id"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCourseDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCourse">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 查看课程详情对话框 -->
    <el-dialog
      title=" 查看课程详情"
      :visible.sync="showCourseDialogVisible"
      width="50%"
      center
    >
      <!-- 内容主体区 -->
      <el-form :model="showForm" label-width="100px">
        <el-form-item label="课程名称">
          <el-input v-model="showForm.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="年级">
          <el-input v-model="showForm.grade" disabled></el-input>
        </el-form-item>
        <el-form-item label="学期">
          <el-input v-model="showForm.semester" disabled></el-input>
        </el-form-item>
        <el-form-item label="学校">
          <el-input v-model="showForm.school" disabled></el-input>
        </el-form-item>
        <el-form-item label="学院">
          <el-input v-model="showForm.college" disabled></el-input>
        </el-form-item>
        <el-form-item label="班级名称">
          <el-input v-model="showForm.title" disabled></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="showCourseDialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>

    <!-- 修改课程对话框 -->
    <el-dialog
      title="修改课程"
      :visible.sync="editCourseDialogVisible"
      width="50%"
      center
    >
      <!-- 内容主体区 -->
      <el-form label-width="100px">
        <el-form-item label="课程名称">
          <el-input v-model="editCourseForm.name"></el-input>
        </el-form-item>
        <el-form-item label="年级">
          <el-select v-model="editCourseForm.grade">
            <el-option label="2020" value="2020"></el-option>
            <el-option label="2021" value="2021"></el-option>
            <el-option label="2022" value="2022"></el-option>
            <el-option label="2023" value="2023"></el-option>
            <el-option label="2024" value="2024"></el-option>
            <el-option label="2025" value="2025"></el-option>
            <el-option label="2026" value="2026"></el-option>
            <el-option label="2027" value="2027"></el-option>
            <el-option label="2028" value="2028"></el-option>
            <el-option label="2029" value="2029"></el-option>
            <el-option label="2030" value="2030"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学期">
          <el-select v-model="editCourseForm.semester">
            <el-option label="2020-2021-1" value="2020-2021-1"></el-option>
            <el-option label="2020-2021-2" value="2020-2021-2"></el-option>
            <el-option label="2021-2022-1" value="2021-2022-1"></el-option>
            <el-option label="2021-2022-2" value="2021-2022-2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-select v-model="editCourseForm.school" @focus="getSchoolList">
            <template v-for="school in schoolList">
              <el-option
                :label="school.name"
                :value="school.id"
                :key="school.id"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>

        <el-form-item label="学院" prop="collegeId">
          <el-select
            v-model="editCourseForm.collegeId"
            @focus="UpdateGetCollegeList"
          >
            <template v-for="college in collegeList">
              <el-option
                :label="college.name"
                :value="college.id"
                :key="college.id"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>

        <el-form-item label="班级名称" prop="title">
          <el-input v-model="editCourseForm.title"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCourseDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="onEditCourse">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 删除课程对话框 -->
    <el-dialog
      title="删除课程"
      :visible.sync="removeCourseDialogVisible"
      width="30%"
      center
    >
      <span>确定删除掉课程:{{ removeCourseForm.name }} 吗?</span>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="removeCourseDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="removeCourse">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 结束课程对话框 -->
    <el-dialog
      title="结束课程"
      :visible.sync="endCourseDialogVisible"
      width="30%"
      center
    >
      <span>确定结束课程:{{ showForm.name }} 吗?</span>

      <span slot="footer" class="dialog-footer">
        <el-button @click="disOnCourse">关 闭</el-button>
        <el-button type="primary" @click="onEndCourse">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  saveCourse,
  removeCourse,
  updateCourse,
  getCourse,
  pageTeacherCourse,
  closeCourse,
  openCourse,
  endCourse,
} from "@/api/course.js";
import {
  pageSchool,
  pageCollege,
  getCollegeById,
} from "@/api/organizationalmanagement.js";
export default {
  data() {
    return {
      //获取课程列表的参数对象
      queryInfo: {
        current: 1,
        size: 5,
        name: "",
      },
      courseList: [],
      total: 0,
      //控制添加课程的对话框的显示与隐藏
      addCourseDialogVisible: false,
      // 添加课程的数据
      addCourseForm: {
        name: "",
        semester: "",
        grade: "",
        collegeId: "",
        school: "",
        title: "",
      },
      // 添加表单的验证规则对象
      addCourseFormRules: {
        name: [
          {
            required: true,
            message: "请输入课程名称",
            trigger: "blur",
          },
        ],
        semester: [
          {
            required: true,
            message: "请输入学期",
            trigger: "blur",
          },
        ],
        grade: [
          {
            required: true,
            message: "请输入年级",
            trigger: "blur",
          },
        ],
        collegeId: [
          {
            required: true,
            message: "请选择学院",
            trigger: "blur",
          },
        ],
        school: [
          {
            required: true,
            message: "请选择大学",
            trigger: "blur",
          },
        ],
        title: [
          {
            required: true,
            message: "请输入班级",
            trigger: "blur",
          },
        ],
      },
      //控制查看课程信息对话框的显示与隐藏
      showCourseDialogVisible: false,
      //查询到的课程信息对象
      showForm: {},
      //控制修该课程对话框的显示与隐藏
      editCourseDialogVisible: false,
      //保存修改的课程信息对象
      editCourseForm: {},
      //删除课程对话框的显示与隐藏
      removeCourseDialogVisible: false,
      //保存删除的课程信息对象
      removeCourseForm: {},
      //结束课程对话框的显示和隐藏
      endCourseDialogVisible: false,

      //下拉框中的学校列表
      schoolList: [],
      //用来记住学校的id
      schoolId: "",
      //下拉框中的学院列表
      collegeList: [],
    };
  },
  created() {
    this.getCourseList();
  },
  methods: {
    //获取学校列表
    async getSchoolList() {
      const { data: res } = await pageSchool(1, 300);
      this.schoolList = res.data.records;
    },
    //添加获取学校对应的学院列表
    async getCollegeList() {
      var id = this.addCourseForm.school;
      const { data: res } = await pageCollege(1, 100, id);
      this.collegeList = res.data.records;
    },

    //修改获取学校对应的学院列表
    async UpdateGetCollegeList() {
      var id = this.editCourseForm.school;
      const { data: res } = await pageCollege(1, 100, id);
      this.collegeList = res.data.records;
    },
    //获取课程列表
    async getCourseList() {
      const { data: res } = await pageTeacherCourse(
        this.queryInfo.current,
        this.queryInfo.size,
        this.queryInfo.name
      );
      this.courseList = res.data.records;

      this.total = res.data.total;
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.size = newSize;
      this.getCourseList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.current = newPage;
      this.getCourseList();
    },
    // 监听添加课程对话框关闭事件
    addCourseDialogClosed() {
      this.$refs.addCourseFormRef.resetFields();
    },
    // 点击确定，增加新课程
    addCourse() {
      this.$refs.addCourseFormRef.validate((valid) => {
        if (!valid) {
          return;
        }
        var organId = null;
        if (this.addCourseForm.collegeId != null) {
          organId = this.addCourseForm.collegeId;
        } else if (this.addCourseForm.school != null) {
          organId = this.addCourseForm.school;
        } else {
          organId = null;
        }
        var addForm = {
          name: this.addCourseForm.name,
          semester: this.addCourseForm.semester,
          grade: this.addCourseForm.grade,
          title: this.addCourseForm.title,
          organId: organId,
        };

        saveCourse(addForm)
          .then((res) => {
            if (res.data.code === 0) {
              this.addCourseDialogVisible = false;
              this.getCourseList();

              return;
            } else {
              this.$message({
                message: "添加失败",
                type: "warning",
              });
            }
          })
          .catch(() => {
            this.$message.error("请求出错");
          });
      });
    },
    //展示查看课程信息详情的对话框
    showDialog(id) {
      getCourse(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.showCourseDialogVisible = true;
          this.showForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //展示编辑课程的对话框
    editCourseDialog(id) {
      getCourse(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.editCourseDialogVisible = true;
          var editList = res.data.data;
          this.editCourseForm = {
            id: editList.id,
            name: editList.name,
            grade: editList.grade,
            semester: editList.semester,
            school: null,
            collegeId: null,
            title: editList.title,
          };
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    //提交修改课程信息
    onEditCourse() {
      var organId = null;
      if (this.editCourseForm.collegeId != null) {
        organId = this.editCourseForm.collegeId;
      } else if (this.editCourseForm.school != null) {
        organId = this.editCourseForm.school;
      } else {
        organId = null;
      }
      var EditCourse = {
        organId: organId,
        grade: this.editCourseForm.grade,
        title: this.editCourseForm.title,
        name: this.editCourseForm.name,
        semester: this.editCourseForm.semester,
      };

      updateCourse(this.editCourseForm.id, EditCourse)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请填完整信息或课程已经结束");
          }
          this.editCourseDialogVisible = false;
          this.editCourseForm = [];
          this.getCourseList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //删除课程提示框展示
    removeDialog(id) {
      getCourse(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.removeCourseDialogVisible = true;
          this.removeCourseForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    // 点击确定删除课程
    removeCourse() {
      var id = this.removeCourseForm.id;

      removeCourse(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.removeCourseDialogVisible = false;
          this.getCourseList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    //结束课程
    endDialog(id) {
      getCourse(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.endCourseDialogVisible = true;
          this.showForm = res.data.data;
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
    //确定结束某个课
    onEndCourse() {
      var id = this.showForm.id;
      endCourse(id)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("请求出错");
          }
          this.endCourseDialogVisible = false;
          this.getCourseList();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    disOnCourse() {
      this.endCourseDialogVisible = false;
      this.getCourseList();
    },

    openChanged(course) {
      if (course.opened == false) {
        closeCourse(course.id);
      } else {
        openCourse(course.id);
      }
    },

    endChanged(course) {
      if (course.ended == true) {
        this.showForm = course;
        this.endCourseDialogVisible = true;
      }
    },
    toTask(id) {
      localStorage.setItem("courseToTaskId", JSON.stringify(id));
      this.$router.push("/course/task");
    },
  },
};
</script>

<style lang="less" scoped>
.breadcrumb {
  margin-bottom: 15px;
  font-size: 15px;
}
.box-card {
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
}
.el-row-table {
  margin-bottom: 15px !important;
}
.el-pagination {
  margin-top: 15px;
}
</style>